package cn.edu.jsu.official.controller;

import cn.edu.jsu.official.pojo.dto.*;
import cn.edu.jsu.official.pojo.vo.ArticleVO;
import cn.edu.jsu.official.pojo.vo.CommonResult;
import cn.edu.jsu.official.pojo.vo.LoginVO;
import cn.edu.jsu.official.pojo.vo.MemberToShowVO;
import cn.edu.jsu.official.service.AdminService;
import cn.edu.jsu.official.utils.DateUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static cn.edu.jsu.official.utils.DateUtil.dateAdd;


/**
 * @author Mo
 * @createTime 2022/3/2 23:06
 * @description
 */
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public CommonResult login(@RequestBody LoginVO loginVO, HttpServletRequest request) {
        return adminService.login(loginVO, request);
    }

    @PostMapping("/uploadProjectAchievement")
    @ApiOperation("上传项目成就")
    public CommonResult uploadProjectAchievement(@RequestParam("articleFile")MultipartFile articleFile,
                                                 @RequestParam("frontCoverFile")MultipartFile frontCoverFile,
                                                 ProjectAchievement projectAchievement) {
        projectAchievement.setCompletedDate(dateAdd(projectAchievement.getCompletedDate(), 1));
        return adminService.uploadProjectAchievement(articleFile, frontCoverFile, projectAchievement);
    }

    @PostMapping("/addProjectInProgressInfo")
    @ApiOperation("添加正在进行的项目信息")
    public CommonResult addProjectInProgressInfo(@RequestParam("frontCoverFile")MultipartFile frontCoverFile,
                                                 ProjectInProgressInfo projectInProgressInfo) {
        projectInProgressInfo.setProjectStartDate(dateAdd(projectInProgressInfo.getProjectStartDate(), 1));
        return adminService.addProjectInProgressInfo(frontCoverFile, projectInProgressInfo);
    }

    @PostMapping("/uploadContestAchievement")
    @ApiOperation("上传竞赛成就")
    public CommonResult uploadContestAchievement(@RequestParam("articleFile")MultipartFile articleFile,
                                                 @RequestParam("frontCoverFile")MultipartFile multipartFile,
                                                 ContestAchievement contestAchievement) {
        contestAchievement.setCompletedDate(dateAdd(contestAchievement.getCompletedDate(), 1));
        return adminService.uploadContestAchievement(articleFile, multipartFile, contestAchievement);
    }

    @PostMapping("/uploadContestInfo")
    @ApiOperation("上传竞赛信息")
    public CommonResult uploadContestInfo(@RequestParam("contestImage") MultipartFile contestImage,
                                          ContestInfo contestInfo) {
        contestInfo.setStartTime(dateAdd(contestInfo.getStartTime(), 1));
        return adminService.uploadContestInfo(contestImage, contestInfo);
    }

    @PostMapping("/uploadCopyright")
    @ApiOperation("上传软著")
    public CommonResult uploadCopyright(@RequestParam("articleFile")MultipartFile articleFile,
                                        @RequestParam("frontCoverFile")MultipartFile frontCoverFile,
                                        CopyrightInfo copyrightInfo) {
        copyrightInfo.setCompletedDate(dateAdd(copyrightInfo.getCompletedDate(), 1));
        return adminService.uploadCopyright(articleFile, frontCoverFile, copyrightInfo);
    }

    @PostMapping("/uploadMemberInfo")
    @ApiOperation("上传成员信息")
    public CommonResult uploadMemberInfo(@RequestParam("memberImage")MultipartFile multipartFile,
                                         MemberInfo memberInfo) {
        System.out.println(memberInfo);
        System.out.println(multipartFile);
        //return new CommonResult(200, "上传成功");
        return adminService.uploadMemberInfo(multipartFile, memberInfo);
    }

    @PostMapping("/addGoalInfo")
    @ApiOperation("添加展望信息")
    public CommonResult addGoalInfo(@RequestBody GoalInfo goalInfo) {
        goalInfo.setDate(dateAdd(goalInfo.getDate(), 1));
        return adminService.addGoalInfo(goalInfo);
    }

    @PostMapping("/deleteProjectAchievement")
    @ApiOperation("删除项目成就")
    public CommonResult deleteProjectAchievement(@RequestBody List<String> projectAchievementIdList) {
        return adminService.deleteProjectAchievement(projectAchievementIdList);
    }

    @PostMapping("/deleteProjectInProgress")
    @ApiOperation("删除正在进行的项目")
    public CommonResult deleteProjectInProgress(@RequestBody List<String> projectIdList) {
        return adminService.deleteProjectInProgress(projectIdList);
    }

    @PostMapping("/deleteContestAchievement")
    @ApiOperation("删除竞赛成就")
    public CommonResult deleteContestAchievement(@RequestBody List<String> contestAchievementIdList) {
        return adminService.deleteContestAchievement(contestAchievementIdList);
    }

    @PostMapping("/deleteContestInfo")
    @ApiOperation("删除竞赛信息")
    public CommonResult deleteContestInfo(@RequestBody List<String> contestInfoIdList) {
        return adminService.deleteContestInfo(contestInfoIdList);
    }

    @PostMapping("/deleteCopyright")
    @ApiOperation("删除软著")
    public CommonResult deleteCopyright(@RequestBody List<String> copyrightIdList) {
        return adminService.deleteCopyright(copyrightIdList);
    }

    @PostMapping("/deleteMemberInfo")
    @ApiOperation("删除成员信息")
    public CommonResult deleteMemberInfo(@RequestBody List<String> memberIdList) {
        return adminService.deleteMemberInfo(memberIdList);
    }

    @PostMapping("/deleteGoalInfo")
    @ApiOperation("删除展望信息")
    public CommonResult deleteGoalInfo(@RequestBody List<String> goalIdList) {
        return adminService.deleteGoalInfo(goalIdList);
    }

    @GetMapping("/countHonor")
    @ApiOperation("查询荣誉总数")
    public CommonResult countHonor() {
        return adminService.countHonor();
    }

    @GetMapping("/countMember")
    @ApiOperation("查询成员人数")
    public CommonResult countMember() {
        return adminService.countMember();
    }


    @GetMapping("/countCopyright")
    @ApiOperation("查询软著数量")
    public CommonResult countCopyright() {
        return adminService.countCopyright();
    }

    @GetMapping("/countContestAchievement")
    @ApiOperation("查询竞赛成就数量")
    public CommonResult countContestAchievement() {
        return adminService.countContestAchievement();
    }

    @GetMapping("/countProjectAchievement")
    @ApiOperation("查询项目成就数量")
    public CommonResult countProjectAchievement() {
        return adminService.countProjectAchievement();
    }

    @PostMapping("/addHistoryProcess")
    @ApiOperation("添加历史进程")
    public CommonResult addHistoryProcess(@RequestBody HistoryProcess historyProcess) {
        historyProcess.setEventDate(dateAdd(historyProcess.getEventDate(), 1));
        return adminService.addHistoryProcess(historyProcess);
    }

    @PostMapping("/deleteHistoryProcess")
    @ApiOperation("删除历史进程")
    public CommonResult deleteHistoryProcess(@RequestBody List<String> historyProcessIdList) {
        System.out.println(historyProcessIdList);
        return adminService.deleteHistoryProcess(historyProcessIdList);
    }

    @PostMapping("/updateProjectInProgress")
    @ApiOperation("修改正在进行中的项目基本信息")
    public CommonResult updateProjectInProgress(@RequestBody ProjectInProgressInfo projectInProgressInfo) {
        projectInProgressInfo.setProjectStartDate(dateAdd(projectInProgressInfo.getProjectStartDate(), 1));
        return adminService.updateProjectInProgress(projectInProgressInfo);
    }

    @PostMapping("/updateContestAchievement")
    @ApiOperation("修改竞赛成就基本信息")
    public CommonResult updateContestAchievement(@RequestBody ContestAchievement contestAchievement) {
        contestAchievement.setCompletedDate(dateAdd(contestAchievement.getCompletedDate(), 1));
        return adminService.updateContestAchievement(contestAchievement);
    }

    @PostMapping("/updateCopyright")
    @ApiOperation("修改软著基本信息")
    public CommonResult updateCopyright(@RequestBody CopyrightInfo copyrightInfo) {
        copyrightInfo.setCompletedDate(dateAdd(copyrightInfo.getCompletedDate(), 1));
        return adminService.updateCopyright(copyrightInfo);
    }

    @PostMapping("/updateMemberInfo")
    @ApiOperation("修改成员基本信息")
    public CommonResult updateMemberInfo(@RequestBody MemberInfo memberInfo) {
        return adminService.updateMemberInfo(memberInfo);
    }

    @PostMapping("/updateHistoryProcess")
    @ApiOperation("修改历史进程")
    public CommonResult updateHistoryProcess(@RequestBody HistoryProcess historyProcess) {
        historyProcess.setEventDate(dateAdd(historyProcess.getEventDate(), 1));
        return adminService.updateHistoryProcess(historyProcess);
    }

    @PostMapping("/updateProjectAchievement")
    @ApiOperation("修改项目成就信息")
    public CommonResult updateProjectAchievement(@RequestBody ProjectAchievement projectAchievement) {
        projectAchievement.setCompletedDate(dateAdd(projectAchievement.getCompletedDate(), 1));
        return adminService.updateProjectAchievement(projectAchievement);
    }

    @PostMapping("/updateContestInfo")
    @ApiOperation("修改竞赛信息")
    public CommonResult updateContestInfo(@RequestBody ContestInfo contestInfo) {
        contestInfo.setStartTime(dateAdd(contestInfo.getStartTime(), 1));
        return adminService.updateContestInfo(contestInfo);
    }

    @PostMapping("/updateGoalInfo")
    @ApiOperation("修改展望信息")
    public CommonResult updateGoalInfo(@RequestBody GoalInfo goalInfo) {
        goalInfo.setDate(dateAdd(goalInfo.getDate(), 1));
        return adminService.updateGoalInfo(goalInfo);
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public CommonResult updatePassword(@RequestBody PwdInfo pwdInfo) {
        return adminService.updatePassword(pwdInfo);
    }

    @PostMapping("/queryUserInfo")
    @ApiOperation("查询个人信息")
    public CommonResult queryUserInfo(@RequestBody UserInfo userInfo) {
        return adminService.queryUserInfo(userInfo);
    }

    @PostMapping("/chooseMemberToDisplay")
    @ApiOperation("选择成员进行首页展示")
    public CommonResult chooseMemberToDisplay(@RequestBody MemberToShowVO memberToShowVO) {
        return adminService.chooseMemberToDisplay(memberToShowVO);
    }

    @PostMapping("/addTechStackInfo")
    @ApiOperation("添加技术栈信息")
    public CommonResult addTechStackInfo(@RequestBody TechStackInfo techStackInfo) {
        return adminService.addTechStackInfo(techStackInfo);
    }

    @PostMapping("/deleteTechStackInfo")
    @ApiOperation("技术栈信息")
    public CommonResult deleteTechStackInfo(@RequestBody List<String> techStackInfoIdList) {
        return adminService.deleteTechStackInfo(techStackInfoIdList);
    }

    @PostMapping("/updateTechStackInfo")
    @ApiOperation("更新技术栈信息")
    public CommonResult updateTechStackInfo(@RequestBody TechStackInfo techStackInfo) {
        return adminService.updateTechStackInfo(techStackInfo);
    }

    @PostMapping("/updateArticle")
    @ApiOperation("修改文章内容")
    public CommonResult updateArticle(@RequestBody ArticleVO articleVO) {
        return adminService.updateArticle(articleVO.getArticleId(),articleVO.getContent());
    }

    // avartar就是memberImage懒得改名字啦
    @PostMapping("/updateAvatar")
    @ApiOperation("修改头像")
    public CommonResult updateAvatar(@RequestParam("avatar")MultipartFile avatar, @RequestParam("memberId")String memberId) {
        return adminService.updateAvatar(avatar, memberId);
    }
}
